package com.yfve.eol.util;

import android.car.hardware.CarYFDiagManager;
import android.content.Intent;
import android.util.Log;

import com.yfve.eol.Constants;
import com.yfve.eol.EolApplication;

import java.util.Arrays;

public class Util {

    private static final String TAG = "EolUtil";

    public static byte intToByte(int val) {
        return (byte) (val & 0xFF);
    }

    public static byte[] intTo2Byte(int val) {
        byte[] b = new byte[2];
        b[0] = (byte) ((val >> 8) & 0xFF);
        b[1] = (byte) (val & 0xFF);
        return b;
    }

    public static byte[] shortToByte(short val) {
        byte[] b = new byte[2];
        b[0] = (byte) ((val >> 8) & 0xFF);
        b[1] = (byte) (val & 0xFF);
        return b;
    }

    /**
     * int到byte[] 由高位到低位
     *
     * @param i 需要转换为byte数组的整行值。
     * @return byte数组
     */
    public static byte[] intTo4Byte(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }

    public static int twoBytesToInt(byte[] bytes, int len) {
        if (bytes.length != 2) {
            return -1;
        }
        return (bytes[0] & 0xFF) << 8 + (bytes[1]);
    }

    public static int middleValue(int value, int min, int max) {
        if (value > max) {
            value = max;
        }
        if (value < min) {
            value = min;
        }
        return value;
    }

    public static String convertByteArray2String(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bytes) {
            if (b == 0x00) {
                break;
            }
            char ch = (char) b;
            stringBuilder.append(ch);
        }
        String str = stringBuilder.toString();
        Log.i(TAG, "convertByteArray2String: " + Arrays.toString(bytes) + ", " + str);
        return str;
    }

    public static void simpleSendBroadCastHelper(String action) {
        Intent intent = new Intent();
        intent.setAction(action);
        EolApplication.getAppContext().sendBroadcast(intent);
    }

    public static void setByteProperty(int id, byte[] bytes) {
        CarYFDiagManager carYFDiagManager = EolApplication.getCarYFDiagManager();
        if (carYFDiagManager == null) {
            Log.e(TAG, "setByteProperty: carYFDiagManager is null");
            return;
        }
        setByteProperty(carYFDiagManager, id, bytes);
    }

    public static void setByteProperty(int id, boolean result) {
        CarYFDiagManager carYFDiagManager = EolApplication.getCarYFDiagManager();
        if (carYFDiagManager == null) {
            Log.e(TAG, "lnay: carYFDiagManager is null");
            return;
        }
        setByteProperty(carYFDiagManager, id, result);
    }


    private static void setByteProperty(CarYFDiagManager carYFDiagManager, int id, byte[] valueByte) {
        Log.i(TAG, "setByteProperty: id = " + id);
        try {
            carYFDiagManager.setByteProperty(id, 0, valueByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setByteProperty(CarYFDiagManager carYFDiagManager, int id, boolean result) {
        Log.i(TAG, "setByteProperty: id = " + id + ", result ? = " + result);
        byte[] bytesVal = new byte[1];
        if (result) {
            bytesVal[0] = Constants.VAL_0X00;
        } else {
            bytesVal[0] = Constants.VAL_0X01;
        }
        try {
            carYFDiagManager.setByteProperty(id, 0, bytesVal);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
